package com.example.pingze.service;

import com.example.pingze.entity.MyInfo;
import com.example.pingze.repository.MyInfoRepository;
import com.example.pingze.utils.RandomNikeName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class MyInfoService {
    @Autowired
    private MyInfoRepository repository;
    //修改用户数据
    public MyInfo update(MyInfo myInfo) {
        MyInfo update = repository.findMyInfoByAccount(myInfo.getAccount());
        if(!StringUtils.isEmpty(myInfo.getNickname())){
            update.setNickname(myInfo.getNickname());
        }
        if(!StringUtils.isEmpty(myInfo.getSexy())){
            update.setSexy(myInfo.getSexy());
        }
        if(!StringUtils.isEmpty(myInfo.getMyTag())){
            update.setMyTag(myInfo.getMyTag());
        }
        if(!StringUtils.isEmpty(myInfo.getMySign())){
            update.setMySign(myInfo.getMySign());
        }
        if(!StringUtils.isEmpty(myInfo.getBirthday())){
            update.setBirthday(myInfo.getBirthday());
        }
        if(!StringUtils.isEmpty(myInfo.getBg())){
            update.setBg(myInfo.getBg());
        }
        MyInfo updateInfo = repository.save(update);
        return updateInfo;
    }
    //通过账号查找对应的用户信息
    public MyInfo findMyInfoByAccount(String account) {
        MyInfo myInfo = repository.findMyInfoByAccount(account);
        return myInfo;
    }
    //在注册账号时，将用户添加到用户信息表，并且为用户生成默认昵称，和唯一二维码
    public MyInfo addMyInfo(String account){
        //注册账号时，为其用户生成默认的昵称
        String nikeName = RandomNikeName.getNickName();
        MyInfo add = new MyInfo(account, nikeName,"未知","诗词大师","平仄带我走进古诗","2024-5-25","D:\\myInfo\\avatar\\1.jpg","D:\\myInfo\\bg\\1.jpg");
        MyInfo info = repository.save(add);
        return info;
    }
    public void delete(MyInfo myInfo) {
        repository.delete(myInfo);
    }

    public Page getMyInfoList(MyInfo myInfo, Pageable pageable) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(!StringUtils.isEmpty(myInfo.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"), myInfo.getAccount()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }



    public MyInfo updateAvatar(MyInfo myInfoByAccount) {
        return repository.save(myInfoByAccount);
    }

    public MyInfo edit(String account, String nickname, String sexy, String myTag, String mySign, String birthday, String avatar,String bg) {
        MyInfo myInfoByAccount = repository.findMyInfoByAccount(account);
        if(nickname != null){
            myInfoByAccount.setNickname(nickname);
        }
        if(sexy != null){
            myInfoByAccount.setSexy(sexy);
        }
        if(myTag != null){
            myInfoByAccount.setMyTag(myTag);
        }
        if(mySign != null){
            myInfoByAccount.setMySign(mySign);
        }
        if(birthday != null){
            myInfoByAccount.setBirthday(birthday);
        }
        if(avatar != null){
            myInfoByAccount.setAvatar(avatar);
        }
        if(bg != null){
            myInfoByAccount.setBg(bg);
        }
        MyInfo save = repository.save(myInfoByAccount);
        return save;
    }

    public MyInfo updateBg(MyInfo myInfoByAccount) {
        return repository.save(myInfoByAccount);
    }
}
